
const util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {

    cancasHidden: false

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data = JSON.parse(options.data)
    let that = this
    // console.log(this.data)
    wx.getSystemInfo({
      success: function(res) {
        // console.log(res)
        that.data.windowHeight = res.windowHeight
        that.data.windowWidth = res.windowWidth
        // that.drowImg()
        that.wxGetImgInfos()
        wx.showLoading({
          title: '图片生成中',
        })
      },
    })
    
  },

  wxGetImgInfos: function () {
    let that = this
    let data = {
      page: 'pages/food/food',
      scene: this.data.foodId
    }
    Promise.all([
        this.wxGetImgInfo(this.data.foodImgUrl),
        this.wxGetImgInfo(this.data.shopHeadImgUrl),
        // this.wxGetACodeInfo()
        util.downloadACode(data)
      ]).then(res => {
        // console.log(res)
        that.drowImg(res[0].path, res[1].path, res[2])
      })
  },

  wxGetImgInfo: function(src) {
    return new Promise(resolve => {
      wx.getImageInfo({
        src: src,
        success: function(res) {
          resolve(res)
        }
      })
    })
  },

  // wxGetACodeInfo: function(src) {
  //   return new Promise(resolve => {
  //     util.getACode().then(data => {
  //       wx.getImageInfo({
  //         src: data,
  //         success: function (res) {
  //           console.log(res)
  //         },
  //         fail: function (res) {
  //           console.log(res)
  //         }
  //       })
  //     })
  //   })
  // },

  drowImg: function (foodImgUrl, shopHeadImgUrl, acodeUrl) {
    // console.log(acodeData)
    let that = this
    let canvasContext = wx.createCanvasContext("share-food", this)

    let canvas_w = 400
    let canvas_h = 700

    canvasContext.setFillStyle("#F8F8F8")
    canvasContext.fillRect(0, 0, canvas_w, canvas_h)

    canvasContext.drawImage(foodImgUrl, 0, 0, canvas_w, 300)

    // 绘制圆角背板
    let x = 20
    let y = 260
    let w = 360
    let h = 140
    let r = 6

    canvasContext.save()
    
    canvasContext.beginPath()
    canvasContext.setFillStyle('#ffffff')
    canvasContext.setLineWidth(1)
    canvasContext.moveTo(x + r, y)
    canvasContext.arcTo(x + w, y, x + w, y + h, r)
    canvasContext.arcTo(x + w, y + h, x, y + h, r)
    canvasContext.arcTo(x, y + h, x, y, r)
    canvasContext.arcTo(x, y, x + w, y, r)
    canvasContext.setShadow(0, 0, 10, '#C8C8C8')
    canvasContext.fill()
    canvasContext.closePath()
    canvasContext.restore()

    // 绘制圆形头像
    let head_w = 120
    let head_h = 120
    let head_x = 30
    let head_y = 270

    canvasContext.save()
    canvasContext.beginPath()
    canvasContext.arc(head_w / 2 + head_x, head_h / 2 + head_y, head_w / 2, 0, Math.PI * 2, false)
    canvasContext.clip()
    canvasContext.drawImage(shopHeadImgUrl, head_x, head_y, head_w, head_h)
    canvasContext.closePath()
    canvasContext.restore()

    canvasContext.setTextAlign('left')
    canvasContext.setFontSize(20)
    canvasContext.setFillStyle('#FF9966')
    canvasContext.setTextBaseline('top')
    let panelTextX = 170
    let foodNameY = 270
    canvasContext.fillText(this.data.foodName, panelTextX, foodNameY - 0.5)
    canvasContext.fillText(this.data.foodName, panelTextX - 0.5, foodNameY)
    canvasContext.fillText(this.data.foodName, panelTextX, foodNameY + 0.5)
    canvasContext.fillText(this.data.foodName, panelTextX + 0.5, foodNameY)

    canvasContext.setFontSize(14)
    canvasContext.setFillStyle('#C8C8C8')
    canvasContext.fillText(this.data.foodStyle, panelTextX, foodNameY + 28)
    canvasContext.fillText(this.data.foodFlavor, panelTextX + 70, foodNameY + 28)
    canvasContext.fillText(this.data.score, panelTextX, foodNameY + 48)
    for (let i = 0; i < 5; i++) {
      if (this.data.score > i) {
        canvasContext.drawImage("../../imgs/star_shine.png", panelTextX + 24 + i * 17, foodNameY + 48, 18, 18)
      } else {
        canvasContext.drawImage("../../imgs/star.png", panelTextX + 24 + i * 17, foodNameY + 48, 18, 18)
      }
    }

    canvasContext.setFontSize(16)
    canvasContext.setFillStyle('#B0B0B0')
    canvasContext.fillText(this.data.shopName, panelTextX, foodNameY + 70)

    canvasContext.setFillStyle('#FF9966')
    canvasContext.setTextBaseline('bottom')
    canvasContext.fillText("￥", panelTextX, foodNameY + 120)
    canvasContext.setFontSize(26)
    canvasContext.fillText(this.data.price, panelTextX + 16, foodNameY + 124 + 0.5)
    canvasContext.fillText(this.data.price, panelTextX + 16, foodNameY + 124 - 0.5)
    canvasContext.fillText(this.data.price, panelTextX + 16 + 0.5, foodNameY + 124)
    canvasContext.fillText(this.data.price, panelTextX + 16 - 0.5, foodNameY + 124)

    canvasContext.setFontSize(14)
    canvasContext.setFillStyle('#B0B0B0')
    canvasContext.setTextBaseline('top')
    
    let lineLength = 24
    let foodDesc = this.data.foodDesc.length > lineLength * 4 ? this.data.foodDesc.substring(0, lineLength * 5) : this.data.foodDesc
    let lineCount = Math.ceil(foodDesc.length / lineLength)
    for (let i = 0; i < lineCount; i++) {
      let start = i * lineLength
      let stop = foodDesc.length - i * lineLength > lineLength ? i * lineLength + lineLength : i * lineLength + foodDesc.length - i * lineLength
      let str = foodDesc.substring(start, stop)
      canvasContext.fillText(str, 30, 415 + i * 20)
    }

    let ma_h = 120
    let ma_w = ma_h
    let ma_x = 140
    let ma_y = 520
    canvasContext.drawImage(acodeUrl, ma_x, ma_y, ma_w, ma_h)
    
    canvasContext.setTextAlign('center')
    let tip_y = ma_y + ma_h + 15
    canvasContext.fillText("长按识别小程序码查看详情", 200, tip_y)

    canvasContext.draw(true, function(){
      that.setData({
        cancasHidden: true
      })
      that.saveCanvasImg()
    })
  },

  saveCanvasImg: function() {
    let that = this
    wx.canvasToTempFilePath({
      canvasId: 'share-food',
      success: function(res) {
        
        // that.setData({
        //   src: res.tempFilePath,
        //   showMask: true
        // })
        // console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function() {
            wx.hideLoading()
            wx.showToast({
              title: '保存成功',
              duration: 1000,
              success: function () {
                setTimeout(function () {
                  wx.redirectTo({
                    url: `../food/food?food_id=${that.data.foodId}`,
                  })
                }, 1000)
              }
            })
          }
        })
      }
    }, this)
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})